using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace TCC.Framework
{
    /// <summary>
    /// 音频管理器
    /// </summary>
    public class AudioManager : BaseManager
    {
        public AudioManager(AudioSource bgSuurece,AudioSource otherSource):base()
        {
            _backgroundSource = bgSuurece;
            _otherSource = otherSource;
        }

        private AudioSource _backgroundSource;
        private AudioSource _otherSource;

        /// <summary>
        /// 是否静音
        /// </summary>
        public bool Mute 
        { 
            get { return _backgroundSource.mute && _otherSource.mute; }
            set
            {
                _backgroundSource.mute = value;
                _otherSource.mute = value;
            }
        }
       
        /// <summary>
        /// 背景音乐音量
        /// </summary>
        public float BackgroundVolme 
        {
            get => _backgroundSource.volume;
            set => _backgroundSource.volume = value;
               
        }

        /// <summary>
        /// 其它音效音量
        /// </summary>
        public float OtherVolme
        {
            get => _otherSource.volume;
            set => _otherSource.volume = value;
        }

        public override void OnInit()
        {
            base.OnInit();
        }

        /// <summary>
        /// 播放背景音乐
        /// </summary>
        /// <param name="clip">音频资源</param>
        /// <param name="isLoop">是否循环</param>
        /// <param name="speed">播放速度</param>
        public void PlayBackgroundMusic(AudioClip clip,bool isLoop=true,float speed=1)
        {
            _backgroundSource.volume = BackgroundVolme;
            PlayMusic(AudioSourceType.BackgroundSource, clip, isLoop, speed);
        }

        /// <summary>
        /// 暂停播放背景音乐
        /// </summary>
        public void PauseBackgroundMusic() => PauseMusic(AudioSourceType.BackgroundSource);

        /// <summary>
        /// 恢复播放背景音乐
        /// </summary>
        public void ResumeBackgroundMusic() => ResumeMusic(AudioSourceType.BackgroundSource);

        /// <summary>
        /// 停止播放背景音乐
        /// </summary>
        public void StopBackgroundMusic() => StopMusic(AudioSourceType.BackgroundSource);
        
        /// <summary>
        /// 播放其它音乐
        /// </summary>
        /// <param name="clip">音频资源</param>
        /// <param name="isLoop">是否循环</param>
        /// <param name="speed">播放速度</param>
         public void PlayOtherMusic(AudioClip clip, bool isLoop = true, float speed = 1)
        {
            _otherSource.volume = OtherVolme;
            PlayMusic(AudioSourceType.OtherSource, clip, isLoop, speed);
        }

        /// <summary>
        /// 暂停播放其它音乐
        /// </summary>
        public void PauseOtherMusic() => PauseMusic(AudioSourceType.OtherSource);

        /// <summary>
        /// 恢复播放其它音乐
        /// </summary>
        public void ResumeOtherMusic() => ResumeMusic(AudioSourceType.OtherSource);

        /// <summary>
        /// 停止播放其它音乐
        /// </summary>
        public void StopOtherMusic() => StopMusic(AudioSourceType.OtherSource);

        /// <summary>
        /// 播放音乐
        /// </summary>
        /// <param name="type">音源类型</param>
        /// <param name="clip">音源</param>
        /// <param name="isLoop">是否循环</param>
        /// <param name="speed">播放速度</param>
        public void PlayMusic(AudioSourceType type, AudioClip clip, bool isLoop = true, float speed = 1)
        {
            AudioSource source = GetAudioSource(type);
            if (clip == null||source==null) return;

            if (source.isPlaying)
                source.Stop();

            source.clip = clip;
            source.loop = isLoop;
            source.pitch = speed;
            source.Play();
        }

        /// <summary>
        /// 停止播放音乐
        /// </summary>
        /// <param name="type">音源类型</param>
        public void StopMusic(AudioSourceType type)
        {
            AudioSource source = GetAudioSource(type);
            if (source&&source.isPlaying)
                source.Stop();
        }

        /// <summary>
        /// 暂停播放音乐
        /// </summary>
        /// <param name="type">音源类型</param>
        public void PauseMusic(AudioSourceType type)
        {
            AudioSource source = GetAudioSource(type);
            source?.Pause();
        }

        /// <summary>
        /// 恢复播放音乐
        /// </summary>
        /// <param name="type">音源类型</param>
        public void ResumeMusic(AudioSourceType type)
        {
            AudioSource source = GetAudioSource(type);
            source?.UnPause();
        }

        private AudioSource GetAudioSource(AudioSourceType type)
        {
            switch(type)
            {
                case AudioSourceType.BackgroundSource:
                    return _backgroundSource;
                case AudioSourceType.OtherSource:
                    return _otherSource;
                default:
                    return null;
            }
        }

        public enum AudioSourceType
        {
            BackgroundSource,
            OtherSource
        }
    }
}
